/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.saving.Branchcfg;
import src.services.finance.BankModel;
import src.services.saving.BranchcfgModel;
import src.services.system.BranchModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class BranchcfgBean implements Serializable {

    private List<Branchcfg> branchcfgs;
    private List<SelectItem> avbBranchs, avbBank;
    private Branchcfg selectedData, insertedData;
    
    private BranchcfgModel helper;
    private BranchModel branchHelper;
    private BankModel bankHelper;
    
    private boolean editMode = false;

    public BranchcfgBean() {
        this.helper = new BranchcfgModel();
        this.bankHelper = new BankModel();
        this.branchHelper = new BranchModel();
        this.branchcfgs = helper.findAll(null);
        this.avbBank = bankHelper.listAll();
        this.avbBranchs = branchHelper.listAll();
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public List<Branchcfg> getBranchcfgs() {
        return this.branchcfgs;
    }
    
    public Branchcfg getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Branchcfg selectedData) {
        this.selectedData = selectedData;
    }
    
    public Branchcfg getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Branchcfg insertedData) {
        this.insertedData = insertedData;
    }

    public List<SelectItem> getAvbBank() {
        return avbBank;
    }

    public void setAvbBank(List<SelectItem> avbBank) {
        this.avbBank = avbBank;
    }

    public List<SelectItem> getAvbBranchs() {
        return avbBranchs;
    }

    public void setAvbBranchs(List<SelectItem> avbBranchs) {
        this.avbBranchs = avbBranchs;
    }
        
    public String getBankName(Integer id) {
        String name = "";
        
        if (id!=null && !id.equals(0)) {
            int idx=0;
            boolean found=false;
            while (idx < avbBank.size() && !found) {
                SelectItem it = avbBank.get(idx);
                found = id.equals(it.getValue()) ? true:false;
                if (found) name = it.getLabel();
                idx++;
            }
        }
        return name;
    }
    
    public String getBranchName(Integer id) {
        String name = "";
        
        if (id!=null && !id.equals(0)) {
            int idx=0;
            boolean found=false;
            while (idx < avbBranchs.size() && !found) {
                SelectItem it = avbBranchs.get(idx);
                found = id.equals(it.getValue()) ? true:false;
                if (found) name = it.getLabel();
                idx++;
            }
        }
        return name;
    }
    
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void newData() {  
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Branchcfg();
    }
    
    public void save() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success;  
             
        Branchcfg data = this.insertedData;
        if (this.insertedData == null) data = this.selectedData;
        
        success = helper.saveData(data);
        if (success && this.editMode!=true) {
            this.branchcfgs.add(this.insertedData);
        }
        
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success;  
        success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.branchcfgs.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatus(success);
        context.addCallbackParam("success", success);  
    }
    
}
